home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: Can be C a so strange language ???. Problem with a function returning a pointer...
- Date: Mon, 22 Jan 96 15:33:07 GMT
- Organization: none
- Message-ID: <822324787snz@genesis.demon.co.uk>
- References: <4e015j$drt@hermes.fundp.ac.be>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4e015j$drt@hermes.fundp.ac.be>
- fmelo@biq.fundp.ac.be "Francisco Melo Ledermann" writes:
-
- >Hi everybody !, I have a problem that for me appears to a really strange
- >one. I have a function that call another function. The function
- >that is called is the next one:
- >
- >struct heavy_atom *Set_Position_AA_Number (int residue_number,
- > struct heavy_atom *position_ptr)
- >{
- > int out;
- >
- > out = 0;
- >
- > while (out != 1)
- > {
- > if ((*position_ptr).res_number != residue_number)
- > {
- > if ((*position_ptr).cooh_ptr != NULL)
-
- The normal more readable way of writing this in C is:
-
- if (position_ptr->cooh_ptr != NULL)
-
- > {
- > position_ptr = (*position_ptr).cooh_ptr;
- > }
- > }
- > else
- > {
- > return (position_ptr);
- > out = 1;
- > }
- > }
- >} /* end of Set_Position_AA_Number function */
-
- This looks rather convoluted and appears to go into an infinite loop if it
- hits the end of the list. Maybe you wanted something like:
-
- {
- while (position_ptr != NULL && position_ptr->res_number != residue_number)
- position_ptr = position_ptr->cooh_ptr;
-
- return position_ptr;
- }
-
- This returns a null pointer if the end of the list is reached without
- a match. It also copes with being passed an empty list.
-
- >The other function that contains the call is the next one:
- >
- >(void) Function_Caller ()
-
- I assume you mean
-
- void Function_Caller ()
-
- >{
- >int counter;
- >
- >struct heavy_atom *first_atom,
- > *actual_atom;
- >
- >..
- >..
- >
- >actual_atom = Set_Position_AA_Number (counter, first_atom);
- >
- >..
- >..
- >
- >} /* end of Function_Caller */
- >
- >
- >The problem is if I debbug and check the current values just in the line
- >before call the function I have:
- >
- >counter = 1
- >(*first_atom).res_number = 1
- >
- >but the function receives:
- >
- >counter = 0
- >(*first_atom).res_number = 1
-
- Most likely this is because you have called Set_Position_AA_Number()
- without a declaration for it in scope. Without a declaration the compiler
- would have assumed that it returned int and then generated the wrong
- call sequence. Make sure you have a line such as:
-
- struct heavy_atom *Set_Position_AA_Number (int residue_number,
- struct heavy_atom *position_ptr);
-
- before Function_Caller();
-
- >I am really surprised about that. I am not a C expert programmer and this
- >is the first time that I construct a function that returns a pointer, and
- >I am sure that the problem is going in that way, because the same
- >function works for me without return a pointer. What's going on here ?.
- >For me this is a really strange thing about C. The FAQ doesn't cover
- >about functions returning pointers (and about funcions in general as a
- >special section) and for that reason I am asking here. If you know what
- >is happening would be great for me !. In advance, thanks a lot for your
- >time and help, best wishes,
-
- There's nothing really special about functions returning pointers, so long
- as the return value doesn't point to a local variable in the funciton
- (which won't exist after the function has returned). This isn't a problem
- with your code however.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-